﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ComputeLight : MonoBehaviour {

	[SerializeField]
	ComputeShader _Shader;

	[SerializeField]
	RenderTexture _Result;

	int _ComputeLight;

	void Start () {
		if (_Result.IsCreated()) _Result.Release();
		_Result.enableRandomWrite = true;
		_Result.Create();
		_ComputeLight = _Shader.FindKernel(nameof(_ComputeLight));
		_Shader.SetTexture(_ComputeLight, nameof(_Result), _Result);
		uint x, y, z;
		_Shader.GetKernelThreadGroupSizes(_ComputeLight, out x, out y, out z);
		var gx = _Result.width / (int)x;
		var gy = _Result.height / (int)y;
		_Shader.Dispatch(_ComputeLight, gx, gy, 1);

		GetComponent<Slider>().onValueChanged.AddListener(val => {
			var radian = val * 2 * Mathf.PI;
			_Shader.SetFloat("_Rotation", radian);
			_Shader.Dispatch(_ComputeLight, gx, gy, 1);
		});
	}
}
